// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DelegateAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Runtime
{
    using System;
    using System.Runtime.Serialization;
    using System.Threading;
    using Microsoft.Robotics.Runtime;
    
    /// <summary>
    /// Consumer agent that publishes to a delegate. Publishing can be delayed via a control port.
    /// </summary>
    /// <typeparam name="T">The type of input/output message.</typeparam>
    [DataContract]
    public class DelegateAgent<T> : ConsumerProducerAgent<T, T>, IControlReceiver<SimpleAgentMessage<WaitHandle>>
        where T : AgentMessage
    {
        /// <summary>
        /// The max size of the queue of messages waiting to be delivered to our Receive method
        /// </summary>
        public const int ReceiverQueueSize = 5;

        /// <summary>
        /// Delegate to call before publishing.
        /// </summary>
        private Action<T> receiveDelegate;

        /// <summary>
        /// Initializes a new instance of the DelegateAgent class.
        /// </summary>
        /// <param name="name">
        /// The name of the agent instance.
        /// </param>
        /// <param name="producer">
        /// The name of the publishing agent instance.
        /// </param>
        /// <param name="receiveDelegate">
        /// A delegate to call prior to publishing a message.
        /// </param>
        /// <param name="receiverQueueSize">The maximum receiver queue size. Optional.</param>
        public DelegateAgent(string name, IProducer<T> producer, Action<T> receiveDelegate, int receiverQueueSize = ReceiverQueueSize)
            : base(name, producer.Name)
        {
            this.receiveDelegate = receiveDelegate;
        }

        /// <summary>
        /// Called to process the subscription.
        /// </summary>
        /// <param name="input">The subscription message.</param>
        [ReceiverConfiguration(QueueSize = ReceiverQueueSize)]
        public override void Receive(T input)
        {
            if (this.receiveDelegate != null)
            {
                this.receiveDelegate(input);
            }

            this.Publisher.Post(input);
        }

        /// <summary>
        /// Called to process the control message.
        /// </summary>
        /// <param name="message">The control message.</param>
        public void ReceiveControl(SimpleAgentMessage<WaitHandle> message)
        {
            message.State.WaitOne();
        }
    }
}
